package com.pet.serve.service;

import com.pet.serve.entity.Customers;
import com.pet.serve.repository.CustomerRepository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CustomerService {

    private final CustomerRepository customerRepository;

    public CustomerService(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public Page<Customers> getAllCustomers(int pageNo, int pageSize, String query) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return customerRepository.findAllByQuery(query, pageable);
    }

    public Customers getCustomerById(Long id) {
        return customerRepository.findById(id).orElse(null);
    }

    public void addOrUpdateCustomer(Customers customer) {
        customerRepository.save(customer);
    }

    public void deleteCustomer(Long id) {
        customerRepository.deleteById(id);
    }

    public void deleteCustomers(List<String> usernames) {
        customerRepository.deleteBatch(usernames);
    }

    public Object exportCustomers(String exportType) {
        return null; // Placeholder
    }

    public List<Customers> getAllAdmins(Customers.UserRole admin) {
        return customerRepository.findByPermission(admin);
    }

    public Page<Customers> getCustomersByLevel(String level, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return customerRepository.findByLevel(level, pageable);
    }

    public Page<Customers> getAll(int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return customerRepository.findAll(pageable);
    }

    public void deleteAdminByUsername(String username) {
        customerRepository.findByUsername(username)
                .ifPresent(customer -> customerRepository.deleteById(customer.getId()));
    }

    public Page<Customers> getVipCustomers(int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return customerRepository.findByLevelContaining("VIP", pageable);
    }


    //修改密码方法，要求验证旧密码
    public boolean updatePassword(String username, String oldPassword, String newPassword) {
        Optional<Customers> optionalCustomer = customerRepository.findByUsername(username);
        if (optionalCustomer.isPresent()) {
            Customers customer = optionalCustomer.get();
            if (customer.getPassword().equals(oldPassword)) { // 应使用加密验证
                customer.setPassword(newPassword); // 保存时加密
                customerRepository.save(customer);
                return true;
            }
        }
        return false;
    }

    //  忘记密码方法，不需要验证旧密码，重置为新密码
    public boolean resetPassword(String username, String newPassword) {
        Optional<Customers> optionalCustomer = customerRepository.findByUsername(username);
        if (optionalCustomer.isPresent()) {
            Customers customer = optionalCustomer.get();
            customer.setPassword(newPassword); // 保存时加密
            customerRepository.save(customer);
            return true;
        }
        return false;
    }

    // 注册用户的方法
    public boolean registerCustomer(Customers customer) {
        // 检查用户名是否已存在
        Optional<Customers> existingCustomer = customerRepository.findByUsername(customer.getUsername());
        if (existingCustomer.isPresent()) {
            return false; // 用户名已存在，注册失败
        }
        // 注册用户，保存到数据库
        customerRepository.save(customer); // 这里应加密密码再保存
        return true; // 注册成功
    }

    public List<Customers> getAllCustomers() {
        return customerRepository.findAll();
    }


    // 登录验证方法
    public boolean authenticate(String username, String password) {
        Optional<Customers> customer = customerRepository.findByUsername(username);
        return customer.map(c -> c.getPassword().equals(password)).orElse(false);
    }

    public Customers getCustomerByUsername(String username) {
        return customerRepository.findByUsername(username).orElse(null);
    }

    public Customers findByUsername(String username) {
        return customerRepository.findByUsername(username).orElse(null);
    }

    public List<Customers> getAllCustomers(int pageNo, int pageSize) {
        return customerRepository.findAll();
    }



    public List<Customers> findByPermission(Customers.UserRole permission, int pageNo, int pageSize) {
        return customerRepository.findByPermission(permission, PageRequest.of(pageNo, pageSize));
    }



    public List<Customers> getCustomerByName(String name, int pageNo, int pageSize) {
        return customerRepository.findByName(name, PageRequest.of(pageNo, pageSize));
    }

    public List<Customers> getCustomerByPermission(Customers.UserRole permission, int pageNo, int pageSize) {
        return customerRepository.findByPermission(permission, PageRequest.of(pageNo, pageSize));
    }

    public List<Customers> getCustomerByRegday(Date regdayStart, Date regdayEnd, int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return customerRepository.findByRegdayBetween(regdayStart, regdayEnd, pageable);
    }

    public List<Customers> getCustomerByBirthday(Date birthdayStart, Date birthdayEnd, int pageNo, int pageSize) {
        Pageable pageable = PageRequest.of(pageNo, pageSize);
        return customerRepository.findByBirthdayBetween(birthdayStart, birthdayEnd, pageable);
    }

    public void deleteCustomerByUsername(String username) {
        customerRepository.findByUsername(username)
                .ifPresent(customer -> customerRepository.deleteById(customer.getId()));
    }


    public boolean validatePassword(String username, String oldPassword) {
        Optional<Customers> customer = customerRepository.findByUsername(username);
        return customer.map(c -> c.getPassword().equals(oldPassword)).orElse(false);
    }

    public boolean updatePassword(String username, String newPassword) {
        Optional<Customers> customer = customerRepository.findByUsername(username);
        if (customer.isPresent()) {
            Customers c = customer.get();
            c.setPassword(newPassword); // 这里应加密密码
            customerRepository.save(c);
            return true;
        }
        return false;
    }
}
